package base.thread.syn;

/**
 * @Author: my_code
 * @Create: 2020-12-08 18:27
 */
public class TicketSale {
    public static void main(String[] args) {
        BuyTicket station = new BuyTicket();
        new Thread(station,"A").start();
        new Thread(station,"B").start();
        new Thread(station,"C").start();
    }

}

class BuyTicket implements Runnable {

    private int ticketNum = 10;
    boolean flag = true;

    @Override
    public void run() {
        while (flag) {
            synchronized (this){
                buy();
            }
        }
    }

    private void buy() {
        if (ticketNum <= 0) {
            flag = false;
            return;
        }
        //模拟延时
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(Thread.currentThread().getName() + "拿到了" + ticketNum--);
    }
}